using Excel = Microsoft.Office.Interop.Excel;
using System;
using System.Collections.Generic;
using System.Data.OleDb;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Windows.Forms;
using System.Globalization;
using System.IO;

namespace ParserColumnasDescripcionMarca
{
    public class CustomExcelData
    {

        public CustomExcelData(DataRow dr)
        {
            try
            {
                GRUPO_COD = (string.IsNullOrEmpty(dr["GRUPO_COD"].ToString())
                    ? int.MinValue
                    : int.Parse(dr["GRUPO_COD"].ToString().Trim()));
            }
            catch
            {
                GRUPO_COD = int.MinValue;
            }            
            try
            {
                FLIA_COD = (string.IsNullOrEmpty(dr["FLIA_COD"].ToString().Trim())
                    ? int.MinValue
                    : int.Parse(dr["FLIA_COD"].ToString().Trim()));
            }
            catch
            {
                FLIA_COD = int.MinValue;
            }            
            try
            {
                SUB_FLIA_COD = (string.IsNullOrEmpty(dr["SUB_FLIA_COD"].ToString().Trim())
                    ? int.MinValue
                    : int.Parse(dr["SUB_FLIA_COD"].ToString().Trim()));
            }
            catch
            {
                SUB_FLIA_COD = int.MinValue;
            }            
            try
            {
                COD = (string.IsNullOrEmpty(dr["COD"].ToString().Trim())
                    ? int.MinValue
                    : int.Parse(dr["COD"].ToString().Trim()));
            }
            catch
            {
                COD = int.MinValue;
            }
            GRUPO_DESC = dr["GRUPO_DESC"].ToString().Trim();
            FLIA_DESC = dr["FLIA_DESC"].ToString().Trim();
            SUB_FLIA_DESC = dr["SUB_FLIA_DESC"].ToString().Trim();
            DESC_COMP = dr["DESC_COMP"].ToString().Trim();
            DESC_LTA = dr["DESC_LTA"].ToString().Trim();
            PRES = dr["PRES"].ToString().Trim();
            try
            {
                MAR_COD = (string.IsNullOrEmpty(dr["MAR_COD"].ToString().Trim())
                    ? int.MinValue
                    : int.Parse(dr["MAR_COD"].ToString().Trim()));
            }
            catch
            {
                MAR_COD = int.MinValue;
            }
            MAR = dr["MAR"].ToString().Trim();
            try
            {
                PRECIO = GetPRECIOField(dr);
            }
            catch
            {
                PRECIO = decimal.MinValue;
            }
            IMP_INT = dr["IMP_INT"].ToString().Trim();
            OF = dr["OF"].ToString().Trim();
            CLAS_LTA = dr["CLAS_LTA"].ToString().Trim();
            V_LTA = dr["V_LTA"].ToString().Trim();

        }

        private decimal GetPRECIOField(DataRow dr)
        {
            if (string.IsNullOrEmpty(dr["PRECIO"].ToString().Trim()))
                return decimal.MinValue;

            string OriValue = dr["PRECIO"].ToString().Trim().ToString(CultureInfo.InvariantCulture);
            string DecSeparator = CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator;
            if (OriValue.Contains(DecSeparator))
            {
                string OnlyDecimalPart = OriValue.Substring(OriValue.IndexOf(DecSeparator) + 1);

                if (OnlyDecimalPart.Count() < 3) //Indicates if have to add zeros                
                    OnlyDecimalPart = OnlyDecimalPart.PadRight(3, '0');
                else if (OnlyDecimalPart.Count() > 3)
                    OnlyDecimalPart = OnlyDecimalPart.Substring(0, 3);

                OriValue = String.Format("{0}{2}{1}", OriValue.Remove(OriValue.IndexOf(DecSeparator)), OnlyDecimalPart, DecSeparator);
                return decimal.Parse(OriValue, NumberStyles.AllowDecimalPoint);
            }
            else
                return decimal.Parse(String.Format("{0}{1}000", OriValue, DecSeparator), NumberStyles.AllowDecimalPoint);
        }
        public int GRUPO_COD { get; private set; }
        public string GRUPO_DESC { get; private set; }
        public int FLIA_COD { get; private set; }
        public string FLIA_DESC { get; private set; }
        public int SUB_FLIA_COD { get; private set; }
        public string SUB_FLIA_DESC { get; private set; }
        public int COD { get; private set; }
        public string DESC_COMP { get; private set; }
        public string DESC_LTA { get; private set; }
        public string PRES { get; private set; }
        public int MAR_COD { get; private set; }
        public string MAR { get; private set; }
        public decimal PRECIO { get; private set; }
        public string IMP_INT { get; private set; }
        public string OF { get; private set; }
        public string CLAS_LTA { get; private set; }
        public string V_LTA { get; private set; }
        public string COMBINADA { get; set; }
    }
}
